<template>
  <div class="GCooperativeSiteDetail">
    <zyInfoPanel title="基本信息" :showUnfoldButton="false" class="baseInfo">
      <div class="baseBox">
        <zy-modal-info :model-info="baseInfo" cell-num="4"></zy-modal-info>
      </div>
    </zyInfoPanel>
    <zyInfoPanel title="业务信息" :showUnfoldButton="false" class="baseInfo">
      <div class="bussinessContainer">
        <div class="line-box">
          <div class="line-item lineall" v-if="type==='add'">
            <div class="line-item-name">负责人：</div>
            <el-switch v-model="legaler_to_leader"></el-switch>
          </div>
          <div class="line-item">
            <div class="line-item-name" :class="legaler_to_leader?'required':''">负责人姓名：</div>
            <zy-input placeholder="请输入负责人姓名" v-model="control_man" v-if="type=='modify'||type=='add'"></zy-input>
            <div v-else>{{control_man}}</div>
          </div>
          <div class="line-item">
            <div class="line-item-name" :class="legaler_to_leader?'required':''">负责人联系方式：</div>
            <zy-input placeholder="请输入负责人联系方式" v-model="control_phone" v-if="type=='modify'||type=='add'"></zy-input>
            <div v-else>{{control_phone}}</div>
          </div>
          <div class="line-item">
            <div class="line-item-name">合作物流企业：</div>
            <zy-button type="primary" @click="addItem" v-if="type=='add'||type=='modify'"
                       :disabled="enterpriseList.length>19">增加
            </zy-button>
          </div>
        </div>
        <div class="area-box">
          <div class="area-item" v-for="(item,index) in enterpriseList" :key="index">
            <el-select v-model="item.id" modalable placeholder="请选择">
              <el-option v-for="option in optionList" :key="option.value" :label="option.label"
                         :value="option.value"></el-option>
            </el-select>
          </div>
          <div class="area-item" v-if="type=='add'||type=='modify'">
            <zy-button class="add" v-if="enterpriseList.length>0" :disabled="enterpriseList.length>19"
                       @click="handlerCas('0')">+
            </zy-button>
            <zy-button class="sub" v-if="enterpriseList.length>0" @click="handlerCas('1')">-</zy-button>
          </div>
        </div>
      </div>
    </zyInfoPanel>
    <zyInfoPanel title="附件信息" :showUnfoldButton="false" class="baseInfo">
      <div class="cooperativeImgContent">
        <div class="imgItem">
          <div class="itemTitle" :class="type=='detail'?'':'required'">营业执照（副本）</div>
          <div class="itemContent">
            <el-upload
                class="upload-demo"
                drag
                action=""
                :httpRequest="uploadFile"
                :data="{type: 'license_img'}"
                accept=".jpg,.jpeg,.png,.bmp,.wbmp,.JPG,.JPEG,.PNG,.BMP,.WBMP"
                :show-file-list="false" :disabled="type=='detail'||license_img.length>0"
            >
              <zy-image v-model="license_img" :readonly="type=='detail'" v-if="license_img"
                        @removeImage="removeImage(license_img)"></zy-image>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                请上传营业执照（副本）
              </div>
            </el-upload>
          </div>
        </div>
        <div class="imgItem card">
          <div class="itemTitle" :class="type=='detail'?'':'required'">法人身份证</div>
          <div class="itemContent">
            <el-upload
                class="upload-demo"
                drag
                action=""
                :httpRequest="uploadFile"
                :data="{type: 'legal_card_pros'}"
                accept=".jpg,.jpeg,.png,.bmp,.wbmp,.JPG,.JPEG,.PNG,.BMP,.WBMP"
                :show-file-list="false" :disabled="type=='detail'||legal_card_pros.length>0"
            >
              <zy-image :readonly="type=='detail'" v-model="legal_card_pros" v-if="legal_card_pros"
                        @removeImage="removeImage(legal_card_pros)"></zy-image>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                请上传身份证正面
              </div>
            </el-upload>
            <el-upload
                class="upload-demo"
                drag
                action=""
                :httpRequest="uploadFile"
                :data="{type: 'legal_card_cons'}"
                accept=".jpg,.jpeg,.png,.bmp,.wbmp,.JPG,.JPEG,.PNG,.BMP,.WBMP"
                :show-file-list="false" :disabled="type=='detail'||legal_card_cons.length>0"
            >
              <zy-image v-model="legal_card_cons" :readonly="type=='detail'" v-if="legal_card_cons"
                        @removeImage="removeImage(legal_card_cons)"></zy-image>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                请上传身份证反面
              </div>
            </el-upload>
          </div>
        </div>
      </div>
    </zyInfoPanel>
    <zyInfoPanel title="监管设置" :showUnfoldButton="false" class="baseInfo">
      <div class="baseBox">
        <zy-modal-info :model-info="setInfo" cell-num="4" v-if="type=='add'||type=='modify'"></zy-modal-info>
        <zy-modal-info :model-info="setInfoDesc" cell-num="4" v-else></zy-modal-info>
      </div>
      <div class="btn-box">
        <zy-button @click="back">{{type=='detail'?'返回':'取消'}}</zy-button>
        <zy-button type="primary" @click="submit" v-if="type=='add'||type=='modify'">提交</zy-button>
      </div>
    </zyInfoPanel>
    <zy-info-panel title="操作日志" :showUnfoldButton="false" class="baseInfo" v-if="type=='detail'">
      <zy-logs logsTitle="" :logs="logList"></zy-logs>
    </zy-info-panel>
  </div>
</template>

<script>
	import {mapGetters} from 'vuex'

	export default {
		name: "GCooperativeSiteDetail",
		props: {},
		data() {
			return {
				loading: false,
				type: '',
				company_id: '',
				baseInfo: [
					{
						title: '企业名称',
						name: 'company_name',
						type: 'input',
						value: '',
						maxlength: '100',
						titleClass: 'required'
					},
					{
						title: '统一社会信用代码',
						name: 'organization_code',
						type: 'input',
						value: '',
						maxlength: '20',
						titleClass: 'required'
					},
					{
						title: '法人姓名',
						name: 'legal_person',
						type: 'input',
						value: '',
						maxlength: '20',
						titleClass: 'required'
					},
					{
						title: '法人联系方式',
						name: 'legal_phone',
						type: 'input',
						value: '',
						maxlength: '11',
						titleClass: 'required'
					},
					{
						title: '法人身份证',
						name: 'legal_card_no',
						type: 'input',
						value: '',
						maxlength: '18',
						titleClass: 'required'
					},
					{
						title: '经营期限',
						name: 'buiss_time',
						type: 'date',
						value: [],
						titleClass: 'required'
					},
					{
						title: '经营地址',
						name: 'company_address',
						type: 'input',
						value: '',
						maxlength: '200',
						titleClass: 'required'
					},
					{
						title: '地址定位',
						name: 'location',
						btnName: '定位',
						type: 'location',
						value: {},
						titleClass: 'required'
					}
				],
				setInfo: [
					{
						title: '    监管辖区',
						name: 'regulated_id',
						type: 'select',
						value: '',
						optionList: [],
						titleClass: 'required'
					},
					{
						title: '监管民警',
						name: 'police_ids',
						type: 'selectMultiple',
						showWord: true,
						value: '',
						optionList: [],
						titleClass: ''
					},
				],
				setInfoDesc: [
					{
						title: '    监管辖区',
						name: 'regulated_name',
						type: 'select',
						value: '',
						optionList: [],
						readonly: true
					}, {
						title: '监管民警',
						name: 'plc_name',
						type: 'select',
						value: '',
						optionList: [],
						readonly: true
					}
				],
				license_img: '',
				legal_card_pros: '',
				legal_card_cons: '',
				legaler_to_leader: false,
				control_man: '',
				control_phone: '',
				fixed_address: '',
				lng: '',
				lat: '',
				logList: [],
				pagination: {
					page_start: 0, //搜索开始页码
					current_page: 0, //当前页码
					page_count: 0, //总页数
					page_size: 10, //每页数量
					total_count: 0 //总数量
				},
				enterprise: '',
				optionList: [],
				enterpriseList: []
			};
		},
		computed: {
			...mapGetters(['getAreaList'])
		},
		watch: {
			legaler_to_leader(value) {
				if (value) {
					this.baseInfo.forEach(item => {
						if (item.name == 'legal_person') {
							this.control_man = item.value;
						}
						if (item.name == 'legal_phone') {
							this.control_phone = item.value;
						}
					})
				} else {
					this.control_man = this.control_phone = '';
				}
			}
		},
		components: {},
		methods: {
			getLogList() {
				this.$getLogs({operation_position: `GCooperativeSiteDetail_${this.company_id}`}).then(res => {
					this.logList = res || [];
				})
			},
			handlerCas(type, index) {
				if (type == '0') {
					this.addItem()
				} else {
					this.enterpriseList.splice(this.enterpriseList.length - 1, 1)
				}
			},
			addItem() {
				if (this.enterpriseList.length < 20)
					this.enterpriseList.push({id: ''})
			},
			getRegionList() {
				if (this.getAreaList.length) {
					this.arrive_region = this.getAreaList;
				} else {
					this.$axios.post(this.$api.arriveRegionList, {}).then(res => {
						let {code, msg, data} = res;
						if (code == 200) {
							this.arrive_region = data;
							this.changeState({areaList: data})
						} else {
							this.$message.error(msg);
						}
					}).catch(ret => {
						this.$message.error(ret.msg || '服务繁忙，请稍后再试');
					});
				}
			},
			//获取详情
			getData() {
				this.loading = true;
				this.$axios.post(this.$api.getCompanyDetail, {company_id: this.company_id, company_type: 2}).then(res => {
					let {code, msg, data} = res;
					this.loading = false
					if (code === 200) {
						let detail = data.detail;
						this.baseInfo = this.baseInfo.filter(item => {
							if (this.type == 'detail') {
								item.readonly = true;
								item.titleClass = '';
							}
							for (let key in detail) {
								if (item.name == 'buiss_time') {
									if (this.type === 'detail') {
										item.value = this.$Timestamp(detail['buiss_time_start'] * 1000) + ' 至 ' + this.$Timestamp(detail['buiss_time_end'] * 1000)
									} else {
										item.value = [detail['buiss_time_start'] * 1000, detail['buiss_time_end'] * 1000]
									}
								} else if (item.name === 'location') {
									item.readonly = false;
									if (this.type == 'detail')
										item.btnName = '查看'
									item.value['address'] = detail.fixed_address || ''
								} else if (item.name === key) {
									item.value = detail[item.name];
								}
							}
							return item
						});

						this.setInfo = this.setInfo.filter(item => {
							if (this.type == 'detail')
								item.readonly = true;
							for (let key in detail) {
								if (item.name == 'police_ids') {
									let arr = [];
									data.company_police_list.forEach(it => {
										arr.push(it.id)
									})
									item.value = arr;
								} else if (item.name === key) {
									item.value = detail[item.name];
								}
							}
							return item
						})
						if (this.type == 'detail') {
							this.setInfoDesc = this.setInfoDesc.filter(item => {
								if (this.type == 'detail')
									item.readonly = true;
								for (let key in detail) {
									if (item.name == 'plc_name') {
										let arr = [];
										data.company_police_list.forEach(it => {
											arr.push(it.sname)
										})
										item.value = arr.join();
									} else if (item.name === key) {
										item.value = detail[item.name];
									}
								}
								return item
							})
						}
						if (data.detail_tansline.length > 0) {
							this.circuit_list = data.detail_tansline.filter(item => {
								for (let val of item) {
									val.id = [val.p_region_id, val.r_region_id, val.region_id]
								}
								return item;
							})
						}
						this.license_img = detail.license_img;
						this.legal_card_pros = detail.legal_card_pros;
						this.legal_card_cons = detail.legal_card_cons;
						this.control_man = detail.control_man;
						this.control_phone = detail.control_phone;
						this.fixed_address = detail.fixed_address;
						this.lng = detail.lng;
						this.lat = detail.lat;
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.loading = false
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				})
			},
			uploadFile(params) {
				let type = params.data.type;
				this.$axios.post(this.$api.uploadFile, {
					isFile: true,
					file: params.file,
					operator: 'goods',
				}).then(res => {
					if (res.code == 200) {
						this.$message.success(res.msg);
						this[type] = res.data.url;
					} else {
						this.$message.error(res.msg);
					}
				})
			},
			removeImage(type) {
				this[type] = '';
			},
			submit() {
				if (this.legaler_to_leader && !this.control_man) return this.$message.error('请输入负责人姓名')
				if (this.legaler_to_leader && !this.control_phone) return this.$message.error('请输入负责人联系方式')
				let postData = {
					company_id: this.company_id,
					company_type: 2,
					type: 0
				};
				this.baseInfo.forEach(item => {
					if (!item.readonly) {
						if (item.name == 'location') {
							postData.fixed_address = item.value.address || this.fixed_address;
							postData.lng = item.value.lng || this.lng
							postData.lat = item.value.lat || this.lat
						} else if (item.name == 'buiss_time') {
							postData.buiss_time_start = item.value[0] / 1000    //去掉后面的毫秒级
							postData.buiss_time_end = item.value[1] / 1000
						} else {
							postData[item.name] = item.value;
						}
					}
				});

				this.setInfo.forEach(item => {
					if (!item.readonly) {
						if (item.name == 'police_ids') {
							postData[item.name] = item.value.join()
						} else {
							postData[item.name] = item.value;
						}
					}
				});
				let arr = [];
				this.enterpriseList.forEach(item => {
					arr.push(item.id)
				})
				postData.company_ids = arr.join();
				postData.license_img = this.license_img;
				postData.legal_card_pros = this.legal_card_pros;
				postData.legal_card_cons = this.legal_card_cons;
				postData.control_man = this.control_man || '';
				postData.control_phone = this.control_phone || '';
				this.$axios.post(this.$api.g_saveCompany, {
					...postData
				}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.$blackRoute({
							path: '/cooperative_site_check_list',
							that: this,
							name: 'GCooperativeSiteCheckList',
							params: {isRefresh: true}
						})
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			back() {
				this.$blackRoute({
					path: '/cooperative_site_list',
					that: this,
				})
			},
			getEnterpriseList() {
				this.$axios.post(this.$api.g_companyList, {
					pagination: {
						page_start: 1,
						page_size: 9999
					}
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.optionList = data.list.filter(item => {
							item.value = item.company_id;
							item.label = item.company_name;
							return item;
						})
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			getPoliceData() {
				this.$axios.post(this.$api.g_listContactPolice, {
					pagination: {
						page_start: 1,
						page_size: 9999
					}
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.setFilterData({
							police_ids: data.list.filter(item => {
								item.value = item.id;
								item.label = item.name
								return item;
							}),
						}, ['police_ids']);
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			getRegulatedData() {
				this.$axios.post(this.$api.g_regulatedList, {
					pagination: {
						page_start: 1,
						page_size: 9999
					}
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						this.setFilterData({
							regulated_id: data.list.filter(item => {
								item.value = item.regulated_id;
								item.label = item.regulated_name
								return item;
							}),
						}, ['regulated_id']);
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			setFilterData(val, nameList) {
				for (let item of this.setInfo) {
					if (nameList.indexOf(item.name) !== -1) {
						item.optionList = val[item.name]
					}
				}
			},
		},
		mounted() {
			this.company_id = this.$route.query.company_id;
			this.type = this.$route.query.type;
			this.getRegionList();
			this.getPoliceData();
			this.getRegulatedData();
			this.getEnterpriseList();
			if (this.type != 'add') {
				this.getData();
			}
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/government/cooperativeSiteManage/siteDetail/index.less">

</style>
